using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI.WebControls;
using CMS.BIZ;

namespace CMS.Web.UI.UserControls
{
    public class CultureEditor : UserControl
    {

        private BizCulture culture;

        protected Repeater repeaterCulture;

        protected TextBox tbCultureName;
        protected TextBox tbCultureCode;
        protected TextBox tbImagePath;

        protected Button bValidate;

        public CultureEditor()
        {
            base.Init += new EventHandler(CultureEditor_Init);
            base.Load += new EventHandler(CultureEditor_Load);
        }

        void CultureEditor_Init(object sender, EventArgs e)
        {
            bValidate.Click += new EventHandler(Validate_Click);
            string delete = Request.QueryString["Delete"];
            if(!string.IsNullOrEmpty(delete)) 
            {
                Page.BizCultureProvider.Delete(int.Parse(delete));
            }
            ReadCulture();
            if (!IsPostBack)
            {
                SetFieldsValue();
                BindCulture();
            }
        }

        void BindCulture()
        {
            repeaterCulture.DataSource = Page.BizCultureProvider.ReadAll();
            repeaterCulture.DataBind();
        }

        void CultureEditor_Load(object sender, EventArgs e)
        {
        }

        private void ReadCulture()
        {
            string cultureId = Request.QueryString["CultureID"];
            if (!string.IsNullOrEmpty(cultureId))
            {
                culture = Page.BizCultureProvider.Read(int.Parse(cultureId));
            }
        }

        private void SetFieldsValue()
        {
            if (culture != null)
            {
                tbCultureCode.Text = culture.CultureCode;
                tbCultureName.Text = culture.CultureName;
                tbImagePath.Text = culture.ImagePath;
            }
        }

        void Validate_Click(object sender, EventArgs e)
        {
            bool insert = false;
            if (culture == null)
            {
                insert = true;
                culture = new BizCulture();
            }
            culture.CultureCode = tbCultureCode.Text;
            culture.CultureName = tbCultureName.Text;
            culture.ImagePath = tbImagePath.Text;
            if (insert) Page.BizCultureProvider.Create(culture);
            else Page.BizCultureProvider.Update(culture);
            BindCulture();
        }


    }
}
